package com.cimr.comm.controller.manager;

import com.cimr.comm.dto.HttpResult;
import com.cimr.comm.dto.LayuiTableData;
import com.cimr.comm.po.Group;
import com.cimr.comm.po.User;
import com.cimr.comm.service.GroupService;
import com.cimr.comm.token.TokenUtil;
import com.cimr.comm.util.IdUtil;
import com.cimr.dto.ZtreeDto;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * Created by liqi on 2018/2/2.
 * liqiwork@qq.com
 */
@Controller
@RequestMapping("/groupManager/ajax")
public class GroupManagerAjaxController {

    @RequestMapping({"getGroupList"})
    @ResponseBody
    public LayuiTableData getGroupList(@RequestParam(required=true,defaultValue="1") Integer page,
                                        @RequestParam(required=false,defaultValue="10") Integer limit,
                                        @RequestParam(required = false) String groupName) {

        LayuiTableData result = new LayuiTableData();

        User user = TokenUtil.getToken();
        if (user!=null && !Objects.equals("",user.getGroupId())) {
            PageHelper.startPage(page, limit);
            List<Group> list = groupService.getGroupListWithName(groupName, user.getGroupId());
            if (list!=null) {
                PageInfo pageInfo = new PageInfo(list);
                result.setData(pageInfo.getList());
                List<Group> list1 = groupService.getGroupListWithName(groupName, user.getGroupId());
                result.setCount(list1.size());
            } else {
                result.setCount(0);
            }
            result.setCode(0);
            result.setMsg("");
        }

        return result;
    }

    @RequestMapping({"getGroupById"})
    @ResponseBody
    public HttpResult getGroupById(@RequestParam(required = true) String id) {
        Group group = groupService.getGroupById(id);
        HttpResult result = new HttpResult(true, "");
        result.setData(group);
        return result;
    }

    @RequiresPermissions(value = {"groupManager:save",
                                "groupManager:saveRole",
                                "cimrFrame:operation:passkey"}, logical = Logical.OR)
    @RequestMapping({"saveGroup"})
    @ResponseBody
    public HttpResult saveGroup(@RequestParam(required = false) String id,
//                               @RequestParam(required = true) String roleKey,
                               @RequestParam(required = true) String groupName,
                               @RequestParam(required = false) String comment,
                               @RequestParam(required = false) Float orderId,
                               @RequestParam(required = true) String parentId,
                               @RequestParam(required = false) String parentName) {
        int resultStatus = 0;
        HttpResult result = new HttpResult(true, "");
        groupName = groupName.trim();
        comment = comment.trim();
        List groupList = groupService.getGroupListByName(groupName, parentId);
        if (groupList.size()>0) {
            boolean isHasGroupName = false;
            for (int i = 0; i < groupList.size(); i++) {
                if (!Objects.equals(((Group)groupList.get(i)).getId(), id)) {
                    isHasGroupName = true;
                    break;
                };
            }

            if (isHasGroupName) {
                result.setSuccess(false);
                result.setError("组织名称:"+groupName+",已经存在.");
            } else {
                resultStatus = groupService.updateGroup(id, groupName, comment, orderId, parentId, parentName);
            }
        } else {
            if (id != null && !"".equals(id)) {
                resultStatus = groupService.updateGroup(id, groupName, comment, orderId, parentId, parentName);
            } else {
                resultStatus = groupService.insertGroup(IdUtil.getId() + "", groupName, comment, orderId, parentId, parentName);
            }
            if (resultStatus==0) {
                result.setSuccess(false);
            }
        }
        return result;
    }

    @RequestMapping({"updateOrderId"})
    @ResponseBody
    public HttpResult updateOrderId(@RequestParam(required = true) String id,
                                    @RequestParam(required = true) Float orderId) {

        int resultStatus = 0;
        resultStatus = groupService.updateOrderId(id, orderId);
        HttpResult result = new HttpResult(true, "");
        if (resultStatus==0) {
            result.setSuccess(false);
        }
        return result;
    }

    @RequiresPermissions(value = {"groupManager:delete",
            "groupManager:deleteGroup",
            "cimrFrame:operation:passkey"}, logical = Logical.OR)
    @RequestMapping({"deleteGroup"})
    @ResponseBody
    public HttpResult deleteGroup(@RequestParam(required = true) String id) {
        int resultStatus = groupService.deleteGroupById(id);
        HttpResult result = new HttpResult(true, "");
        if (resultStatus == 0) {
            result.setSuccess(false);
            result.setError("删除错误：还有子组织、账号或角色与其关联。");
        }
        return result;
    }

    @RequestMapping({"getGroupZtree"})
    @ResponseBody
    public HttpResult getGroupZtree(String id) {
        HttpResult result = new HttpResult(true, "");
        // 获取可选项
        List<Group> allGroupList = groupService.getGroupList();
        List<ZtreeDto> ztreeList = new ArrayList<ZtreeDto>();
        for (int i = 0;i<allGroupList.size();i++){
            ZtreeDto ztreeDto = new ZtreeDto();
            ztreeDto.setId(allGroupList.get(i).getId());
            ztreeDto.setpId(allGroupList.get(i).getParentId());
            ztreeDto.setParent(true);
            ztreeDto.setName(allGroupList.get(i).getGroupName());
            ztreeList.add(ztreeDto);
        }
        result.setData(ztreeList);
        return result;
    }

    @Autowired
    private GroupService groupService;
}
